package ss.lianft.tools;

import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DBStyle;
import org.beetl.sql.core.db.MySqlStyle;
import org.beetl.sql.core.kit.GenKit;
import org.beetl.sql.core.kit.StringKit;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.ext.gen.GenConfig;
import org.beetl.sql.ext.gen.MapperCodeGen;

import java.io.File;
import java.io.FileWriter;
import java.util.Set;

/**
 * 指定数据库名
 * 批量生成
 * Model 和 Mapper生成
 * beetlSQL
 */
public class GenTools {
    private final static String driverClass = "com.mysql.jdbc.Driver";
    private final static String url_database = "jdbc:mysql://localhost:3306/susong";
    private final static String username = "root";
    private final static String password =  "";

    public static void main(String[] args) {
        ConnectionSource source = ConnectionSourceHelper.getSimple(driverClass, url_database, username, password);
        DBStyle mysql = new MySqlStyle();
            // sql语句放在classpagth的/sql 目录下
        SQLLoader loader = new ClasspathLoader("/sql");
        // 数据库命名跟java命名一样，所以采用DefaultNameConversion，还有一个是UnderlinedNameConversion，下划线风格的，
        UnderlinedNameConversion nc = new  UnderlinedNameConversion();
        // 数据源
        SQLManager sqlManager = new CustomSQLManager(mysql,loader,source,nc,new Interceptor[]{new DebugInterceptor()});
        GenConfig.initTemplate("/pojo.btl");
        GenConfig config = new GenConfig();
        config.preferBigDecimal(true);
        config.setBaseClass("ss.lianft.model.Model");
        try {
            Set<String> tables = sqlManager.getMetaDataManager().allTable();
            tables.forEach(table->{
                try {
                    sqlManager.genPojoCode(table,"ss.lianft.model",config);
                    sqlManager.genSQLFile(table);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
